﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Webdiver;

namespace AnyOffice.general.email
{
    public partial class outbox : BasePage
    {        public void ChangePage(object src, PageChangedEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.BindData();
        }
        public string formatSize(long size)
        {
            return StringHelper.FormatSize(size);
        }
        public string formatStatus(int deleteFlag, bool readFlag)
        {
            if (deleteFlag == 1)
            {
                return "<img title=\"收件人已删除\" src=\"/images/email_delete.gif\">";
            }
            if (!readFlag)
            {
                return "<img title=\"收件人未读\" src=\"/images/email_close.gif\">";
            }
            return "<img title=\"收件人已读\" src=\"/images/email_open.gif\">";
        }
        private void BindData()
        {
            DataTable table = new EmailDao().OutboxSelectPaged(base.Conn, base.User.Identity.Name, this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, false);
            this.rptEmail.DataSource = table;
            this.rptEmail.DataBind();
            this.litNavCount.Text = "<span class=\"small1\">共&nbsp;<span class=\"big4\">" + this.AspNetPager1.RecordCount.ToString() + "</span>&nbsp;封邮件";
            this.litNavPager.Text = "<span class=\"small1\">当前为第" + this.AspNetPager1.CurrentPageIndex.ToString() + "页，共" + this.AspNetPager1.PageCount.ToString() + "页，每页最多" + this.AspNetPager1.PageSize.ToString() + "封</span>";
            this.ViewState["CurrentRowNums"] = table.Rows.Count;
            this.ViewState["CurrentPage"] = this.AspNetPager1.CurrentPageIndex.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.AspNetPager1.PageSize = new ConfigManager().PageSize;
                DataTable table = new EmailDao().OutboxSelectPaged(base.Conn, base.User.Identity.Name, this.AspNetPager1.PageSize, 1, true);
                if (int.Parse(table.Rows[0][0].ToString()) == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litNavCount.Visible = false;
                    this.litNavPager.Visible = false;
                    this.litMessage.Text = base.ShowMessage("发件箱无邮件");
                    return;
                }
                this.AspNetPager1.RecordCount = int.Parse(table.Rows[0][0].ToString());
                this.BindData();
                this.btnDelete.Attributes.Add("onclick", "return delete_mail();");
            }
            if (this.Page.IsPostBack)
            {
                int num = 0;
                EmailDao dao2 = new EmailDao();
                foreach (RepeaterItem item in this.rptEmail.Items)
                {
                    HtmlInputCheckBox box = (HtmlInputCheckBox)item.FindControl("EmailId");
                    if ((box != null) && box.Checked)
                    {
                        dao2.Delete(base.Conn, int.Parse(box.Value), EmailDao.DeleteType.Outbox, base.User.Identity.Name);
                        num++;
                    }
                }
                if (num == Convert.ToInt32(this.ViewState["CurrentRowNums"].ToString()))
                {
                    DataTable table2 = dao2.OutboxSelectPaged(base.Conn, base.User.Identity.Name, this.AspNetPager1.PageSize, 1, true);
                    this.AspNetPager1.RecordCount = int.Parse(table2.Rows[0][0].ToString());
                    if (int.Parse(table2.Rows[0][0].ToString()) == 0)
                    {
                        this.pnlMain.Visible = false;
                        this.litNavCount.Visible = false;
                        this.litNavPager.Visible = false;
                        this.litMessage.Text = base.ShowMessage("发件箱无邮件");
                    }
                    else
                    {
                        if (this.AspNetPager1.CurrentPageIndex > 1)
                        {
                            this.AspNetPager1.CurrentPageIndex--;
                        }
                        this.BindData();
                    }
                }
            }
        }
 
    }
}
